package com.yanqu.road.server.cmd.activity.peakstronghold.cross;

import com.yanqu.road.entity.activity.peakstronghold.config.PeakStrongHoldConfig;
import com.yanqu.road.entity.enums.eChatContentType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.eMessageType;
import com.yanqu.road.entity.log.LogPeakHoldMammonHelp;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.PeakStrongholdWarProto;
import com.yanqu.road.pb.worldmessage.WorldMessageProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.peakstronghold.PeakStrongholdModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.peakstronghold.PeakStrongholdActivityMgr;
import com.yanqu.road.server.manger.activity.strongholdwar.StrongholdActivityMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.player.WorldMessageMgr;
import com.yanqu.road.server.pb.WorldMessagePb;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;

@Cmd(code = GameProtocol.S_PEAK_STRONGHOLD_MAMMON_HELP_NOTIFY, desc = "")
public class CrossPeakStrongholdMammonHelpNotifyCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        PeakStrongHoldConfig config = PeakStrongholdActivityMgr.getConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            return;
        }
        int activityId = config.getActivityInfo().getActivityId();
        PeakStrongholdWarProto.CrossPeakStrongholdMammonHelpRespMsg msg = PeakStrongholdWarProto.CrossPeakStrongholdMammonHelpRespMsg.parseFrom(packet.getBytes());
        int goodsId = msg.getGoodsId();
        ret = msg.getRet();
        int mammonType = msg.getMammonType();
        PeakStrongholdWarProto.PeakMammonHelpRespMsg.Builder resp = PeakStrongholdWarProto.PeakMammonHelpRespMsg.newBuilder();
        resp.setRet(ret);
        Property consume = new Property(goodsId, BigInteger.ONE);
        player.getModule(PreReduceModule.class).restorePreReduce(PropertyHelper.parsePropertyToString(consume));
        if (ret == 0) {
            player.getModule(CurrencyModule.class).removeCurrency(consume, eLogMoneyType.PeakStrongholdActivity, eLogMoneyType.PeakStrongholdActivityMammonHelp);
            PeakStrongholdWarProto.CrossPeakStrongholdMammonHelpDataMsg result = msg.getResult();
            Property killReward = new Property();
            for (int i = 0; i < result.getKillNum(); i++) {
                killReward.addProperty(PeakStrongholdActivityMgr.getEliminateKillReward());
            }
            player.getModule(CurrencyModule.class).addCurrency(killReward, eLogMoneyType.PeakStrongholdActivity, eLogMoneyType.PeakStrongholdActivityMammonHelp);

            PeakStrongholdWarProto.PeakMammonHelpDataMsg.Builder dataMsg = PeakStrongholdWarProto.PeakMammonHelpDataMsg.newBuilder();
            dataMsg.setKillReward(PropertyHelper.parsePropertyToString(killReward));
            dataMsg.setAttackUnionNum(result.getAttackUnionNum());
            dataMsg.setKillNum(result.getKillNum());
            dataMsg.setTotalDamage(result.getTotalDamage());
            dataMsg.addAllDamageDetail(result.getDamageDetailList());
            Property useReward = StrongholdActivityMgr.getMammonUseReward(goodsId);
            player.getModule(CurrencyModule.class).addCurrency(useReward, eLogMoneyType.PeakStrongholdActivity, eLogMoneyType.PeakStrongholdActivityMammonHelp);
            dataMsg.setUseReward(PropertyHelper.parsePropertyToString(useReward));
            // 发聊天公告
            resp.setData(dataMsg);
            player.getModule(PeakStrongholdModule.class).setTotalDamage(new BigInteger(result.getMemberTotalDamage()));
            noticeAllServer(player, activityId, mammonType, result);
            Map<String, Integer> remainBlood = new HashMap<>();
            result.getBloodDetailList().forEach(bloodTemp -> {
                remainBlood.put(bloodTemp.getUnionId(), bloodTemp.getBlood());
            });

            Property totalReward = new Property();
            totalReward.addProperty(killReward);
            totalReward.addProperty(useReward);
            AutoLogMgr.add(new LogPeakHoldMammonHelp(activityId, config.getRound(), player.getUserInfo().getUnionUid(), player.getUserId(),
                    result.getTotalDamage(), result.getGroup(), mammonType, remainBlood, PropertyHelper.parsePropertyToString(totalReward)));
        }
        player.sendPacket(ClientProtocol.U_PEAK_STRONGHOLD_ELIMINATION_MAMMON_HELP, resp);
    }

    private void noticeAllServer(GamePlayer player, int acitvityId, int mammonType, PeakStrongholdWarProto.CrossPeakStrongholdMammonHelpDataMsg result) {
        String content = WorldMessageMgr.encodeContent(mammonType, result.getAttackUnionNum(), result.getKillNum(), result.getTotalDamage(),result.getAttackUnionName());
        WorldMessageProto.WorldMessageTempMsg.Builder talkMsg = WorldMessagePb.parseWorldMessageTempMsg(player.getUserInfo(), eMessageType.PeakStrongholdMammonHelp.getValue(), eChatContentType.Common.getContentType(), content, System.currentTimeMillis(), acitvityId, null);
        WorldMessageMgr.sendCrossActivityChat(talkMsg);
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
